package com.atguigu.service.impl;

import com.atguigu.entity.PlatformPropertyKey;
import com.atguigu.entity.PlatformPropertyValue;
import com.atguigu.mapper.PlatformPropertyKeyMapper;
import com.atguigu.mapper.PlatformPropertyValueMapper;
import com.atguigu.service.PlatformPropertyKeyService;
import com.atguigu.service.PlatformPropertyValueService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 属性表 服务实现类
 * </p>
 *
 * @author GuoJH
 * @since 2021-08-21
 */
@Service
public class PlatformPropertyKeyServiceImpl extends ServiceImpl<PlatformPropertyKeyMapper, PlatformPropertyKey> implements PlatformPropertyKeyService {
    @Autowired
    PlatformPropertyValueService propertyValueService;
    //根据分类的id获取平台属性对应的key值
    @Override
    public List<PlatformPropertyKey> getPlatformPropertyByCategoryId(Long category1Id, Long category2Id, Long category3Id) {
        List<PlatformPropertyKey> propertyKeyList = baseMapper.getPlatformPropertyByCategoryId(category1Id, category2Id, category3Id);

        return propertyKeyList;
    }
    //添加平台属性key
    @Override
    public void savePlatformProperty(PlatformPropertyKey platformPropertyKey) {
        if (platformPropertyKey.getId() != null){//有id为修改 没有则添加
            baseMapper.updateById(platformPropertyKey);
        }else{
            baseMapper.insert(platformPropertyKey);
        }
        QueryWrapper<PlatformPropertyValue> wrapper = new QueryWrapper<>();
        wrapper.eq("property_key_id",platformPropertyKey.getId());
        propertyValueService.remove(wrapper);//添加时候 为了防止重复添加  将所有value删除
        List<PlatformPropertyValue> propertyValueList = platformPropertyKey.getPropertyValueList();
        if (propertyValueList != null){
            for (PlatformPropertyValue platformPropertyValue : propertyValueList) {
                platformPropertyValue.setPropertyKeyId(platformPropertyKey.getId());
//                propertyValueService.save(platformPropertyValue);
            }
            propertyValueService.saveBatch(propertyValueList);//再批量添加
        }
    }
    //根据skuId获取平台属性
    @Override
    public List<PlatformPropertyKey> getPlatformPropertyBySkuId(Long skuId) {
        return baseMapper.getPlatformPropertyBySkuId(skuId);
    }
}
